package com.quanyan.stadium.service.commonComponent.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.stadium.biz.BizGameSystemDao;
import com.quanyan.stadium.biz.redis.UserCategoryScoreCacheDao;
import com.quanyan.stadium.component.cache.MyRedisTemplate;
import com.quanyan.stadium.component.cache.StadiumRedisConstants;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.*;
import com.quanyan.stadium.entity.vo.StadiumNotifyMsg;
import com.quanyan.stadium.entity.vo.UpdateGameRadar;
import com.quanyan.stadium.entity.vo.VDefinedGameSchedule;
import com.quanyan.stadium.entity.vo.VGameEventCollect;
import com.quanyan.stadium.enums.GameSystemConfigEnum;
import com.quanyan.stadium.enums.GameTypeConfigEnum;
import com.quanyan.stadium.mapper.*;
import com.quanyan.stadium.service.*;
import com.quanyan.stadium.service.commonComponent.BaseService;
import com.quanyan.stadium.service.commonComponent.DataReductionService;
import com.quanyan.stadium.service.commonComponent.GameMQProduct;
import com.quanyan.stadium.service.commonComponent.GameMsgPush;
import com.quanyan.stadium.service.gameLevelScore.GameLevelScoreApi;
import com.quanyan.stadium.service.gameRules.GameSystemServiceEight;
import com.quanyan.user.response.RespUserInfoBase;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.quanyan.stadium.service.gameEventScore.*;
import java.util.*;

/**
 * @author xiaoxiao
 * @date 16/4/11-14:42
 */

@Service("dataReductionService")
public class DataReductionServiceImpl implements DataReductionService {

    private final static Logger logger = LoggerFactory.getLogger(DataReductionServiceImpl.class);

    @Autowired
    private BizGameSystemDao bizGameSystemDao;

    @Autowired
    private GradeIntegralService gradeIntegralService;

    @Autowired
    private VStadiumUserDefinedMapper vStadiumUserDefinedMapper;

    @Autowired
    private TbUserGameScheduleMapper tbUserGameScheduleMapper;

    @Autowired
    private TbGameEventMapper tbGameEventMapper;

    @Autowired
    private TbGameScheduleMapper tbGameScheduleMapper;

    @Autowired
    private BaseService baseService;

    @Autowired
    private TbGameCollectMapper tbGameCollectMapper;

    @Autowired
    private TbGameEventScoreMapper tbGameEventScoreMapper;

    @Autowired
    private TbUserGameCollectMapper tbUserGameCollectMapper;

    @Autowired
    private TbUserScoreCollectMapper tbUserScoreCollectMapper;

    @Autowired
    private TbPointsMapper tbPointsMapper;

    @Autowired
    private TbUserIntegralMapper tbUserIntegralMapper;

    @Autowired
    private UserCategoryScoreCacheDao cacheService;

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @Autowired
    private RadarChartService radarChartService;

    @Autowired
    private PointsService pointsService;

    @Autowired
    private GradeIntegralService getGradeIntegralService;

    @Autowired
    private GameSystemServiceEight gameSystemServiceEight;

    @Autowired
    private GameScoreService gameScoreService;

    @Autowired
    private GameMQProduct gameMQProduct;

    @Autowired
    private GameMsgPush gameMsgPush;

    @Autowired
    private GameLevelScoreApi gameLevelScoreApi;



    private final static int DATE_REFRESH_NUM = 100;

    //一小时推荐
    private final static int ONE_HOUR_MIN = 60;

    @Override
    public void gameBeforeOneHourEventStartRemind(int min) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andStartTimeBetween(DateUtils.addTime(new Date(), (ONE_HOUR_MIN - min), DateUtils.TIME_TYPE_MINUTE), DateUtils.addTime(new Date(), ONE_HOUR_MIN, DateUtils.TIME_TYPE_MINUTE));
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        List<TbGameEvent> tbGameEventList = tbGameEventMapper.selectByExample(example);
        if (null!=tbGameEventList && tbGameEventList.size() >0) {
            for (TbGameEvent tbGameEvent:tbGameEventList) {
                RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.getGameEventStartKey(tbGameEvent.getId()));
                boolean flag = myRedisTemplate.setnxWithTime(redisKey, ONE_HOUR_MIN * 1000);
                if (!flag) {gameMsgPush.pushBeforeOneHourGameStartMsgToUser(tbGameEvent, this.queryUidsFromTbGameEvent(tbGameEvent));

                }
            }
        }
    }

    @Override
    public void gameEventStartRemind(int min) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andStartTimeBetween(new Date(), DateUtils.addTime(new Date(), min, DateUtils.TIME_TYPE_MINUTE));
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        List<TbGameEvent> tbGameEventList = tbGameEventMapper.selectByExample(example);
        if (null!=tbGameEventList && tbGameEventList.size() >0) {
            for (TbGameEvent tbGameEvent:tbGameEventList) {
                RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.getBeforeOneHourGameEventStartKey(tbGameEvent.getId()));
                boolean flag = myRedisTemplate.setnxWithTime(redisKey, ONE_HOUR_MIN * 1000);
                if (!flag) {
                    List<Integer> uids = this.queryUidsFromTbGameEvent(tbGameEvent);
                    if (null!=uids && uids.size()>0) {
                        gameMsgPush.pushGameStartMsgToUser(tbGameEvent, this.queryUidsFromTbGameEvent(tbGameEvent));
                    }
                }
            }
        }
    }

    @Override
   public void updateUserScoreAndIntegral(int min) {
        logger.info("开始实时刷新汇总用户的栏目积分和评级积分");
        Date oldTime = DateUtils.addTime(new Date(), (0-min), DateUtils.TIME_TYPE_MINUTE);
        /*栏目积分汇总*/
        TbPointsExample example1 = new TbPointsExample();
        TbPointsExample.Criteria c1 = example1.createCriteria();
        c1.andUpdateTimeGreaterThanOrEqualTo(oldTime);
        List<TbPoints> points = tbPointsMapper.selectByExample(example1);
        if(null!=points && points.size() > 0) {
            this.updateUserCategoryScore(points);
        }
        /*评价积分汇总*/
        TbUserIntegralExample example2 = new TbUserIntegralExample();
        TbUserIntegralExample.Criteria c2 = example2.createCriteria();
        c2.andUpdateTimeGreaterThanOrEqualTo(oldTime);
        List<TbUserIntegral> integrals =tbUserIntegralMapper.selectByExample(example2);
        if(null!=integrals && integrals.size() > 0) {
            this.updateUserIntegralScore(integrals);
        }
        logger.info("结束实时刷新汇总用户的栏目积分和评级积分");
    }


    private void updateUserCategoryScore(List<TbPoints> points){
        Set<Integer> ids =new HashSet<>();
        for (TbPoints tb : points) {
            ids.add(tb.getUid());
        }
        TbUserScoreCollectExample ex = new TbUserScoreCollectExample();
        TbUserScoreCollectExample.Criteria cr = ex.createCriteria();
        cr.andUidIn(new ArrayList<Integer>(ids));
        List<TbUserScoreCollect> collects = tbUserScoreCollectMapper.selectByExample(ex);
        Map<Integer, Map<Integer, TbUserScoreCollect>> mapUserScore = new HashMap<>();
        Map<Integer, TbUserScoreCollect> temp = new HashMap<>();
        if (null!=collects && collects.size() > 0) {
            for (TbUserScoreCollect tb:collects) {
                temp = mapUserScore.get(tb.getUid());
                if (temp==null || temp.size() < 1) {
                    temp = new HashMap<>();
                }
                temp.put(tb.getCategoryId(), tb);
                mapUserScore.put(tb.getUid(), temp);
            }
        }

        for (TbPoints tb : points) {
            TbUserScoreCollect collect = new TbUserScoreCollect();
            if (null!=mapUserScore && null!=mapUserScore.get(tb.getUid()) && null!= mapUserScore.get(tb.getUid()).get(tb.getCategoryId())) {
                collect.setId(mapUserScore.get(tb.getUid()).get(tb.getCategoryId()).getId());
                collect.setScore(tb.getScore());
                collect.setCityId(tb.getCityId());
                tbUserScoreCollectMapper.updateByPrimaryKeySelective(collect);
            }else {
                collect.setCityId(tb.getCityId());
                collect.setCategoryId(tb.getCategoryId());
                collect.setScore(tb.getScore());
                collect.setUid(tb.getUid()) ;
                tbUserScoreCollectMapper.insertSelective(collect);
            }
        }
    }


    private void updateUserIntegralScore(List<TbUserIntegral> integrals){
        Set<Integer> ids =new HashSet<>();
        for (TbUserIntegral tb : integrals) {
            ids.add(tb.getUserId());
        }
        TbUserScoreCollectExample ex = new TbUserScoreCollectExample();
        TbUserScoreCollectExample.Criteria cr = ex.createCriteria();
        cr.andUidIn(new ArrayList<Integer>(ids));
        List<TbUserScoreCollect> collects = tbUserScoreCollectMapper.selectByExample(ex);
        Map<Integer, Map<Integer, TbUserScoreCollect>> mapUserScore = new HashMap<>();
        Map<Integer, TbUserScoreCollect> temp = new HashMap<>();
        if (null!=collects && collects.size() > 0) {
            for (TbUserScoreCollect tb:collects) {
                temp = mapUserScore.get(tb.getUid());
                if (temp==null || temp.size() < 1) {
                    temp = new HashMap<>();
                }
                temp.put(tb.getCategoryId(), tb);
                mapUserScore.put(tb.getUid(), temp);
            }
        }
        for (TbUserIntegral tb : integrals) {
            TbUserScoreCollect collect = new TbUserScoreCollect();
            if (null!=mapUserScore && null!=mapUserScore.get(tb.getUserId()) && null!= mapUserScore.get(tb.getUserId()).get(tb.getGameCategoryId())) {
                collect.setId(mapUserScore.get(tb.getUserId()).get(tb.getGameCategoryId()).getId());
                collect.setIntegral(tb.getIntegral());
                collect.setGrade(getGradeIntegralService.getUserGradeInfo(tb.getUserId(), tb.getGameCategoryId()).getGrade());
                tbUserScoreCollectMapper.updateByPrimaryKeySelective(collect);
            }else {
                collect.setIntegral(tb.getIntegral());
                collect.setGrade(getGradeIntegralService.getUserGradeInfo(tb.getUserId(),tb.getGameCategoryId()).getGrade());
                collect.setUid(tb.getUserId());
                collect.setCategoryId(tb.getGameCategoryId());
                tbUserScoreCollectMapper.insertSelective(collect);
            }
        }
    }

    @Override
    public void updateGameEventMsg() {
        logger.info("开始实时刷新官方事和pk赛事的数据统计");
        // 官方赛事更新,每次更新将以前的数据逻辑删除
        TbGameCollectExample example = new TbGameCollectExample();
        TbGameCollect temp = new TbGameCollect();
        temp.setIsDelete(GameConstants.IS_DELETE);
        tbGameCollectMapper.updateByExampleSelective(temp, example);

        //更新官方赛事
        List<VGameEventCollect> resultTemp = vStadiumUserDefinedMapper.selectVGameEventCollect();
        if (null!=resultTemp && resultTemp.size()>0) {
            for (VGameEventCollect collect : resultTemp) {
                TbGameCollect tbGameCollect = new TbGameCollect();
                tbGameCollect.setGameCategoryId(collect.getCategoryId());
                tbGameCollect.setCityId(collect.getDistrictId());
                tbGameCollect.setDistrictId(collect.getDistrictId());
                tbGameCollect.setGameEventNum(collect.getGameEventNum());
                tbGameCollect.setGametype(GameConstants.GAME_TYPE_OFFICIAL);
                tbGameCollectMapper.insertSelective(tbGameCollect);
            }
        }

        //更新pk赛事
        List<VGameEventCollect> pkResultTemp = vStadiumUserDefinedMapper.selectVpkGameEventCollect();
        if (null!=pkResultTemp && pkResultTemp.size()>0) {
            for (VGameEventCollect collect : pkResultTemp) {
                TbGameCollect tbGameCollect = new TbGameCollect();
                tbGameCollect.setGameCategoryId(collect.getCategoryId());
                tbGameCollect.setGameEventNum(collect.getGameEventNum());
                tbGameCollect.setGametype(GameConstants.GAME_TYPE_PK);
                tbGameCollectMapper.insertSelective(tbGameCollect);
            }
        }
        logger.info("结束实时刷新官方事和pk赛事的数据统计");
    }

    @Override
    public void officialGameEventTeamJoinUpdate(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        if (tbGameEvent == null || tbGameEvent.getId()<1) {
            return;
        }
        if (tbGameEvent.getGameEventType().intValue() != GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_NGKC.getId()) {
            return;
        }
        logger.info("排位赛事结束,更新排行榜开始, 当前赛事:{}", tbGameEvent.getId());
        TbGameEvent parentGameEvent = bizGameSystemDao.selectTbGameEvent(tbGameEvent.getParentId());
        if (null == parentGameEvent || parentGameEvent.getSignStartTime().before(new Date())) { //报名已经开始,不计入总分
            return;
        }
        List<TbGameEventScore> tbGameEventScores = bizGameSystemDao.queryTbGameEventScoreList(tbGameEvent.getId());
        Map<Integer, TbGameEventTeam> teamScore = new HashMap<>();
        List<Integer> teamIds = new ArrayList<>();
        if (null!=tbGameEventScores && tbGameEventScores.size()>0) {
            List<TbGameEventTeam> tbGameEventTeamList = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getParentId(), null, null, null);
            if (null!=tbGameEventTeamList && tbGameEventTeamList.size()>0) {    //更新+插入
                for (TbGameEventTeam tbGameEventTeam:tbGameEventTeamList) {
                    if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                        teamScore.put(tbGameEventTeam.getClubId(), tbGameEventTeam);
                        teamIds.add(tbGameEventTeam.getClubId());
                    }else {
                        teamScore.put(tbGameEventTeam.getUid(), tbGameEventTeam);
                        teamIds.add(tbGameEventTeam.getUid());
                    }
                }
                for (TbGameEventScore tbGameEventScore:tbGameEventScores) {
                    if (teamIds.contains(tbGameEventScore.getClubId()) || teamIds.contains(tbGameEventScore.getUid())) {
                        TbGameEventTeam team = new TbGameEventTeam();
                        if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                            team = teamScore.get(tbGameEventScore.getClubId());
                        } else {
                            team = teamScore.get(tbGameEventScore.getUid());
                        }
                        team.setScore(team.getScore() + tbGameEventScore.getScore());
                        bizGameSystemDao.updateTbGameEventTeam(team);
                    } else {
                        TbGameEventTeam team = new TbGameEventTeam();
                        team.setIsJoinAccess(GameConstants.IS_NOT_DELETE);   //默认无权限
                        team.setIsForfeit(GameConstants.IS_NOT_DELETE);  //默认不弃权
                        team.setIsDelete(GameConstants.IS_DELETE);     //默认均不报名
                        team.setGameEventId(tbGameEvent.getParentId());
                        team.setType(tbGameEvent.getGameTeamType());
                        if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                            team.setClubId(tbGameEventScore.getClubId());

                        } else {
                            team.setUid(tbGameEventScore.getUid());
                        }
                        team.setScore(tbGameEventScore.getScore());
                        bizGameSystemDao.insertTbGameEventTeam(team);
                    }
                    logger.info("排位赛事结束,非首次插入场馆赛事积分排行榜, 当前赛事:{},当前队伍id:{}", tbGameEvent.getId(), tbGameEventScore.getGameEventTeamId());
                }
            }else { //直接插入
                for (TbGameEventScore tbGameEventScore:tbGameEventScores) {
                    TbGameEventTeam team = new TbGameEventTeam();
                    team.setIsJoinAccess(GameConstants.IS_NOT_DELETE);   //默认无权限
                    team.setIsForfeit(GameConstants.IS_NOT_DELETE);  //默认不弃权
                    team.setIsDelete(GameConstants.IS_DELETE);     //默认均不报名
                    team.setGameEventId(tbGameEvent.getParentId());
                    team.setType(tbGameEvent.getGameTeamType());
                    if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                        team.setClubId(tbGameEventScore.getClubId());
                    }else {
                        team.setUid(tbGameEventScore.getUid());
                    }
                    team.setScore(tbGameEventScore.getScore());
                    bizGameSystemDao.insertTbGameEventTeam(team);
                    logger.info("排位赛事结束,首次插入场馆赛事积分排行榜, 当前赛事:{},当前队伍id:{}", tbGameEvent.getId(), tbGameEventScore.getGameEventTeamId());
                }
            }
        }
        logger.info("排位赛事结束,更新排行榜结束, 当前赛事:{}", tbGameEvent.getId());
    }

    @Override
    public void officialGameEventCollectMsg(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        if (tbGameEvent == null || tbGameEvent.getId()<1) {
            return;
        }
        logger.info("个人数据汇总, 当前赛事id:{}", tbGameEvent.getId());
        Set<Integer> uIds = new HashSet<>();
        if (tbGameEvent.getType().equals(GameConstants.PERSONAL_CREATE)) {
            logger.info("个人赛事结束,队伍类型是用户,开始产生用户的名次");
            this.personGameEventFinishSetPersonTop(tbGameEvent, uIds);
        }else {
            //汇总用户的积分
            TbGameEventScoreExample example = new TbGameEventScoreExample();
            TbGameEventScoreExample.Criteria criteria = example.createCriteria();
            criteria.andGameEventIdEqualTo(tbGameEvent.getId());
            criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
            List<TbGameEventScore> tbGameEventScores = tbGameEventScoreMapper.selectByExample(example);
            if(null == tbGameEventScores || tbGameEventScores.size()<1) {
                return;
            }
            if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {
                logger.info("赛事结束,队伍类型是俱乐部,开始产生俱乐部的名次");
                this.gameEventFinishSetClubTop(tbGameEventScores, tbGameEvent, uIds);
            }else if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_PERSON)) {
                logger.info("赛事结束,队伍类型是用户,开始产生用户的名次");
                this.gameEventFinishSetPersonTop(tbGameEventScores, tbGameEvent, uIds);
            }else {
                ;
            }

        }
    }

    @Override
    public void officialGameEventPushMsg(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        if (tbGameEvent == null || tbGameEvent.getId()<1) {
            return;
        }

        logger.info("赛事结束,开始一些推送, 当前赛事:{}", tbGameEvent.getId());

        //汇总用户的积分
        TbGameEventScoreExample example = new TbGameEventScoreExample();
        TbGameEventScoreExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(tbGameEvent.getId());
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        example.setOrderByClause("rank asc");
        List<TbGameEventScore> tbGameEventScores = tbGameEventScoreMapper.selectByExample(example);
        if(null == tbGameEventScores || tbGameEventScores.size()<1) {
            return;
        }
        logger.info("赛事结束,开始一些推送, 当前赛事:{}, 获取赛事用户的排名列表,大小:{}", tbGameEvent.getId(), tbGameEventScores.size());

        Set<Integer> uIds = new HashSet<>();
        Map<Integer, List<Integer>> clubUids = new HashMap<>();
        if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {
            List<Integer> clubIds = new ArrayList<>();
            for (TbGameEventScore tbGameEventScore : tbGameEventScores) {  //获取俱乐部
                clubIds.add(tbGameEventScore.getClubId());
            }
            clubUids = baseService.queryClubIds(clubIds);  //获取俱乐部代表队
            if (clubUids == null || clubUids.size()<1) {
                return;
            }
            for (Map.Entry<Integer,  List<Integer>> entry : clubUids.entrySet()) {
                List<Integer> tempIds = entry.getValue();
                if (tempIds!=null && tempIds.size() > 0) {
                    for (Integer temp : tempIds) {
                        uIds.add(temp);
                    }
                }
            }
        }else if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_PERSON)) {
            for (TbGameEventScore tbGameEventScore : tbGameEventScores) {
                uIds.add(tbGameEventScore.getUid());
            }

        }else {
            ;
        }
        logger.info("赛事结束,开始一些推送, 当前赛事:{}, 封装用户信息和俱乐部信息", tbGameEvent.getId());

        // 赛事完结推送给所有人
        gameMsgPush.pushGameFinishPushMsgToUser(tbGameEvent, new ArrayList<>(uIds));

        //赛事排名推送
        if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            gameMsgPush.pushTeamTypeClubMsgToUserFirst(clubUids.get(tbGameEventScores.get(0).getClubId()), tbGameEvent);
            gameMsgPush.pushTeamTypeClubMsgToUserSecond(clubUids.get(tbGameEventScores.get(1).getClubId()), tbGameEvent);
        }else {
            gameMsgPush.pushTeamTypePersonMsgToUserFirst(tbGameEventScores.get(0).getUid(), tbGameEvent);
            gameMsgPush.pushTeamTypePersonMsgToUserSecond(tbGameEventScores.get(1).getUid(), tbGameEvent);
        }

        //赛事结束给俱乐部推送MQ信息
        if (tbGameEvent.getGameEventType().intValue() != GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_NGKC.getId() &&
                tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            this.officialGameEventClubJoin(tbGameEvent);
        }

        logger.info("赛事结束,一些推送操作结束, 当前赛事:{}", tbGameEvent.getId());
    }


    @Override
    public void officialGameEventTeamJoin() {
        /**
         * 自动刷新赛事的报名状态
         *
         * */
        logger.info("开始实时刷新处于报名阶段的官方赛事");
        List<TbGameEvent> tbGameEvents = bizGameSystemDao.selectTbGameEventScoreStatus(false);
        if (null!=tbGameEvents && tbGameEvents.size()>0) {
            if (tbGameEvents.size() > DATE_REFRESH_NUM) {
                tbGameEvents = tbGameEvents.subList(0, DATE_REFRESH_NUM-1);
            }
            List<Integer> uids = new ArrayList<>();
            List<Integer> clubIds = new ArrayList<>();
            for (TbGameEvent tbGameEvent :  tbGameEvents) {
                List<TbGameEventTeam> list = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getId(), GameConstants.IS_DELETE);  //

                for (int i=0;i<tbGameEvent.getTeamLimitNum(); i++) {
                    TbGameEventTeam tbGameEventTeam = list.get(i);
                    uids.add(tbGameEventTeam.getUid());
                    clubIds.add(tbGameEventTeam.getClubId());
                    tbGameEventTeam.setIsJoinAccess(GameConstants.IS_DELETE);   //有权限
                    bizGameSystemDao.updateTbGameEventTeam(tbGameEventTeam);

                }
                TbGameEvent temp = new TbGameEvent();
                temp.setId(tbGameEvent.getId());
                temp.setRecordScore(GameConstants.IS_DELETE);
                bizGameSystemDao.updateTbGameEvent(temp);

                //开始送获得资格的用户
                this.pushJoinAccessUser(uids, clubIds, tbGameEvent);
            }
        }
        logger.info("结束实时刷新处于报名阶段的官方赛事");
    }

    @Override
    public void officialComplentGameEventTeamJoin() {
        /**
         *  报名结束,开始刷新是否存在,推位赛的用户
         *
         * */
        logger.info("开始实时刷新处于退位阶段的官方赛事");
        List<TbGameEvent> tbGameEvents = bizGameSystemDao.selectTbGameEventScoreStatus(true);
        if (null!=tbGameEvents && tbGameEvents.size()>0) {
            if (tbGameEvents.size() > DATE_REFRESH_NUM) {
                tbGameEvents = tbGameEvents.subList(0, DATE_REFRESH_NUM-1);
            }
            for (TbGameEvent tbGameEvent :  tbGameEvents) {
                List<Integer> uids = new ArrayList<>();
                List<Integer> clubIds = new ArrayList<>();
                if (tbGameEvent.getInventoryLevel().equals(0)) {
                    break;
                }
                List<TbGameEventTeam> tbGameTeams = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getId(), null);  //取出是所有的
                if (tbGameEvent.getInventoryLevel().intValue() == 0) {      //人已经报满,直接返回
                    return;
                }
                int i=0;
                for (TbGameEventTeam vDefinedGameTeam : tbGameTeams) {
                    i++;
                    if (i>tbGameEvent.getTeamLimitNum() && i<=(tbGameEvent.getTeamLimitNum()+tbGameEvent.getInventoryLevel())) {
                        uids.add(vDefinedGameTeam.getUid());
                        clubIds.add(vDefinedGameTeam.getClubId());
                        TbGameEventTeam team = new TbGameEventTeam();
                        team.setId(vDefinedGameTeam.getId());
                        team.setIsJoinAccess(GameConstants.IS_DELETE);   //默认有权限报名
                        bizGameSystemDao.updateTbGameEventTeam(team);
                    }
                }

                TbGameEvent temp = new TbGameEvent();
                temp.setId(tbGameEvent.getId());
                temp.setRecordScore(GameConstants.DEFULT_NUM_2);
                bizGameSystemDao.updateTbGameEvent(temp);
                //开始送获得资格的用户
                this.pushJoinAccessUser(uids, clubIds, tbGameEvent);
            }
        }
        logger.info("结束实时刷新处于退位阶段的官方赛事");
    }

    @Override
    public void gameEventScoreRecord(TbGameEvent tbGameEvent, List<Integer> ids) {
        logger.info("赛事结束时,开始进行该赛事积分的统计, 当前赛事id:{}", tbGameEvent.getId());
        Map<Integer, TbGameRoundScore> map = new HashMap<>();
        List<TbGameRoundScore> tbGameRoundScoreList = bizGameSystemDao.selectTbGameRoundScoreByGameRoundIds(ids);
        for (TbGameRoundScore tb : tbGameRoundScoreList) {   //根据下一轮覆盖上一轮的逻辑进行数据的替换,淘汰赛如此,其他赛事均算最后一轮
            TbGameRoundScore temp = map.get(tb.getGameEventTeamId());
            if (null!=temp&&temp.getId()!=null&&temp.getId()>0) {
                if (tb.getProducts() > temp.getProducts()) {
                    temp = tb;
                }
            }else {
                temp = tb;
            }
            map.put(tb.getGameEventTeamId(), temp);
        }
        List<TbGameEventTeam> tbGameEventTeamList = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getId(), null, null, null);
        List<TbGameEventScore> tbGameEventScores = new ArrayList<>();   //封装用户信息
        for (TbGameEventTeam tbGameEventTeam : tbGameEventTeamList) {
            TbGameEventScore gameEventTeamScore = new TbGameEventScore();
            gameEventTeamScore.setCategoryId(tbGameEvent.getCategoryId());
            gameEventTeamScore.setPlaceId(tbGameEvent.getPlaceId());
            gameEventTeamScore.setGameEventId(tbGameEvent.getId());
            gameEventTeamScore.setGameEventTeamId(tbGameEventTeam.getId());
            gameEventTeamScore.setType(tbGameEventTeam.getType());
            gameEventTeamScore.setClubId(tbGameEventTeam.getClubId());
            gameEventTeamScore.setUid(tbGameEventTeam.getUid());
            gameEventTeamScore.setMemberTime(tbGameEventTeam.getCreateTime());
            gameEventTeamScore.setProducts(map.get(tbGameEventTeam.getId()).getProducts());
            gameEventTeamScore.setWins(map.get(tbGameEventTeam.getId()).getWins());
            gameEventTeamScore.setTies(map.get(tbGameEventTeam.getId()).getTies());
            gameEventTeamScore.setFails(map.get(tbGameEventTeam.getId()).getFails());
            gameEventTeamScore.setOriginalScore(map.get(tbGameEventTeam.getId()).getScore());
            tbGameEventScores.add(gameEventTeamScore);
        }
        Collections.sort(tbGameEventScores, new Comparator<TbGameEventScore>() {   //先按照分数排序,然后按照时间排序
            public int compare(TbGameEventScore o1, TbGameEventScore o2) {
                if (o2.getOriginalScore() - o1.getOriginalScore() > 0) {
                    return 1;
                }else if (o2.getOriginalScore() - o1.getOriginalScore() < 0) {
                    return -1;
                }else {
                    return (int)(o2.getMemberTime().getTime() - o1.getMemberTime().getTime());
                }
            }
        });


        //排位赛分数插入,按大小
        logger.info("赛事结束,给赛事下的队伍,分配积分开始,当前赛事id:{}", tbGameEvent.getId());
        //this.gameEventFinishWinScore(tbGameEventScores, tbGameEvent);
    }

    @Override
    public void gameEventFinishWinScore(List<TbGameEventScore> tbGameEventScores, TbGameEvent tbGameEvent){
        int top1 = 0,top2 =0, i =0;
        if (tbGameEvent.getGameSystemType().intValue() == GameSystemConfigEnum.CONFIG_GAME_TYPE_PK.getId()) {
            logger.info("开始计算8人制双打的队伍积分排名");
            List<TbGameRound> tbGameRoundList = bizGameSystemDao.selectTbGameRoundByGameEventId(tbGameEvent.getId());
            Collections.sort(tbGameRoundList, new Comparator<TbGameRound>() {   //按轮次的先后排序,id最大的为最后一轮
                public int compare(TbGameRound o1, TbGameRound o2) {
                    return o1.getId().compareTo(o2.getId());
                }
            });
            List<TbGameSchedule> scheduleList = bizGameSystemDao.selectTbGameScheduleByGameRoundId(tbGameRoundList.get(0).getId(), null, null);
            List<Integer> newTeamIds = new ArrayList<>();
            for (TbGameSchedule tbGameSchedule : scheduleList) {
                if (tbGameSchedule.getIsWin().intValue() == GameConstants.SCHEDULE_LOSE.intValue()) {
                    Gson gson = new Gson();
                    List<Integer> tempTeam = gson.fromJson(tbGameSchedule.getTeamListIndex(), new TypeToken<List<Integer>>() {
                    }.getType());
                    List<Integer> tempLocation = gson.fromJson(tbGameSchedule.getTeamListIndex(), new TypeToken<List<Integer>>() {
                    }.getType());
                    newTeamIds = gameSystemServiceEight.eightChangeLocation(tempTeam, tempLocation);
                }
            }
            Map<Integer, Integer> mapRank = new HashMap<>();
            for (int j=0;j<newTeamIds.size(); j++) {
                mapRank.put(newTeamIds.get(j), j);
            }

            for (TbGameEventScore score : tbGameEventScores) {  //进行积分计算

                if (mapRank.get(score.getGameEventTeamId()) == 0) {
                    if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                        top1 = score.getClubId();
                    }else {
                        top1 = score.getUid();
                    }
                }else if (mapRank.get(score.getGameEventTeamId()) == 1) {
                    if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                        top2 = score.getClubId();
                    }else {
                        top2 = score.getUid();
                    }
                }else {

                }
                score.setRank(mapRank.get(score.getGameEventTeamId())+1);
                if (score.getOriginalScore() != null && score.getOriginalScore() > 0.0) {
                    score.setScore(score.getOriginalScore() + score.getProducts() * 1);
                }else {
                    score.setScore(score.getProducts() * 1.0);
                }
                bizGameSystemDao.insertTbGameEventScore(score);
            }
        }else {

            for (TbGameEventScore score : tbGameEventScores) {  //进行积分计算
                if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                    if (i==0) {
                        top1 = score.getClubId();
                    }else if (i==1) {
                        top2 = score.getClubId();
                    }
                }else {
                    if (i==0) {
                        top1 = score.getUid();
                    }else if (i==1) {
                        top2 = score.getUid();
                    }
                }
                if (score.getOriginalScore() != null && score.getOriginalScore() > 0.0) {
                    score.setScore(score.getOriginalScore() + score.getProducts() * 1);
                }else {
                    score.setScore(score.getProducts() * 1.0);
                }
                score.setRank(i+1);
                i++;
                bizGameSystemDao.insertTbGameEventScore(score);
            }
        }

        //排位赛统计积分总和
        if (tbGameEvent.getGameEventType().intValue() == GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_NGKC.getId()) {
            logger.info("排位赛事结束,开始将赛事的队伍录入到官方赛的榜单中,当前赛事id:{}", tbGameEvent.getId());
            //this.officialGameEventTeamJoinUpdate(tbGameEvent);
        }

        if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            gameMsgPush.pushTeamTypeClubMsgToUserFirst(null, tbGameEvent);
            gameMsgPush.pushTeamTypeClubMsgToUserSecond(null, tbGameEvent);
        }else {
            gameMsgPush.pushTeamTypePersonMsgToUserFirst(top1, tbGameEvent);
            gameMsgPush.pushTeamTypePersonMsgToUserSecond(top2, tbGameEvent);
        }
        logger.info("赛事结束,积分排名和推送, 当前赛事:{}", tbGameEvent.getId());
    }

    @Override
    public void gameScheduleScoreRecord(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        TbGameRound tbGameRound = stadiumNotifyMsg.getCurrentGameRound();
        if (tbGameEvent == null || tbGameEvent.getId()<1 || tbGameRound == null || tbGameRound.getId() < 1) {
            return;
        }
        logger.info("开始更新用户评级积分完毕, 当前赛事:{}, 当前轮次:{}", tbGameEvent.getId(), tbGameRound.getId());
        List<VDefinedGameSchedule> gameScheduleMsg = bizGameSystemDao.selectVDefinedGameScheduleByGameRoundId(tbGameRound.getId(), null, null);
        Map<Integer, List<VDefinedGameSchedule>> vMap = new HashMap<>();
        for (VDefinedGameSchedule vDefinedGameSchedule : gameScheduleMsg) {   //数据整理
            List<VDefinedGameSchedule> temp = vMap.get(vDefinedGameSchedule.getGameScheduleId());
            if (null==temp || temp.size()<1){
                temp = new ArrayList<>();
            }
            temp.add(vDefinedGameSchedule);
            vMap.put(vDefinedGameSchedule.getGameScheduleId(), temp);
        }

        if(tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            //clubLevelScoreService.collectFLScoreIncrement(null);
            //clubLevelScoreService.collectFLScoreIncrement();
        }else if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_PERSON.intValue()) {

            for (Map.Entry<Integer, List<VDefinedGameSchedule>> entry : vMap.entrySet()) {  //分数统计
                List<VDefinedGameSchedule> vDefinedGameSchedules = entry.getValue();
                if (vDefinedGameSchedules.size() != 2) {
                    //TODO 双打比赛不考虑等积分  只考虑单人对打
                    return;
                }
                List<Integer> selfUids = new ArrayList<>();
                List<Integer> opponentUids = new ArrayList<>();

                VDefinedGameSchedule own = new VDefinedGameSchedule();
                VDefinedGameSchedule opp = new VDefinedGameSchedule();


                for (VDefinedGameSchedule  ve : vDefinedGameSchedules) {   //记录分数
                    if (ve.getType().intValue() == 0) {
                        own = ve;
                    }else {
                        opp = ve;
                    }
                }
                gameLevelScoreApi.collectLScoreIncrement(own.getUid(), opp.getUid(), own.getFinalScore(), tbGameEvent.getId(),
                        tbGameEvent.getCategoryId(), vDefinedGameSchedules.get(0).getGameScheduleId());
            }
        }


        /*
        logger.info("开始等级分计算,遍历当前轮次下的赛程信息, 当前赛事:{}, 当前轮次:{}", tbGameEvent.getId(), tbGameRound.getId());
        for (Map.Entry<Integer, List<VDefinedGameSchedule>> entry : vMap.entrySet()) {  //分数统计

            List<VDefinedGameSchedule> vDefinedGameSchedules = entry.getValue();

            QuaryUserGradePoint quaryIntegral= new QuaryUserGradePoint();
            quaryIntegral.setCategoryId(tbGameEvent.getCategoryId());
            quaryIntegral.setGameScheduleId(entry.getKey());
            quaryIntegral.setGradePointLevel(baseService.getGameGradeLevelById(tbGameEvent.getCategoryId(), tbGameEvent.getGameGradeLevelType().intValue()).getScore());

            QuaryGameLevel quaryGameLevel = new QuaryGameLevel();
            quaryGameLevel.setCategoryType(tbGameEvent.getCategoryId());
            if (tbGameEvent.getGameEventType().intValue() == GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_NGKC.getId()) {
                quaryGameLevel.setGameType(IntegralConstants.POINT_RACE);
            }else {
                quaryGameLevel.setGameType(IntegralConstants.FINALS);
            }
            quaryGameLevel.setGameFormat(IntegralConstants.UNKNOWN_TYPE);
            quaryGameLevel.setGroup(IntegralConstants.HOST_BELING);

            List<Integer> selfUids = new ArrayList<>();
            List<Integer> opponentUids = new ArrayList<>();
            for (VDefinedGameSchedule  ve : vDefinedGameSchedules) {   //记录分数
                quaryIntegral.setRecordScore(ve.getFinalScore());
                quaryIntegral.setGameResult(ve.getIsWin());
                if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_PERSON)) {
                    if (ve.getType().intValue() == 0) {
                        selfUids.add(ve.getUid());
                    }else {
                        opponentUids.add(ve.getUid());
                    }
                }else {
                    if (ve.getType().intValue() == 0) {
                        quaryIntegral.setSelfTeamId(ve.getClubId());
                    }else {
                        quaryIntegral.setOpponentTeamId(ve.getClubId());
                    }
                }
            }
            quaryIntegral.setSelfUserIDs(selfUids);
            quaryIntegral.setOpponentUserIDs(opponentUids);
            if (null!=selfUids && selfUids.size()>0) {
                gradeIntegralService.calculateGradePoint(quaryIntegral);
            }

        }
        */
        logger.info("结束更新用户评级积分完毕, 当前赛事:{}, 当前轮次:{}", tbGameEvent.getId(), tbGameRound.getId());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout=10, rollbackFor = Exception.class)
    public void gameScheduleCategoryScoreRecord(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        if (tbGameEvent == null || tbGameEvent.getId()<1) {
            return;
        }
        logger.info("赛事结束,开始计算栏目积分, 当前赛事:{}", tbGameEvent.getId());

        //查询用户积分录入大小
        TbGameEventScoreExample example = new TbGameEventScoreExample();
        TbGameEventScoreExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(tbGameEvent.getId());
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        List<TbGameEventScore> tbGameEventScores = tbGameEventScoreMapper.selectByExample(example);
        if(null == tbGameEventScores || tbGameEventScores.size()<1) {
            return;
        }

        //获取用户信息和俱乐部信息组合
        Set<Integer> uIds = new HashSet<>();
        Map<Integer, List<Integer>> clubUidMap= new HashMap<>();
        if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {
            List<Integer> clubIds = new ArrayList<>();
            for (TbGameEventScore tbGameEventScore : tbGameEventScores) {  //获取俱乐部
                clubIds.add(tbGameEventScore.getClubId());
            }
            clubUidMap = baseService.queryClubIds(clubIds);  //获取俱乐部代表队
            if (clubUidMap == null || clubUidMap.size()<1) {
                return;
            }
            for (Map.Entry<Integer,  List<Integer>> entry : clubUidMap.entrySet()) {
                List<Integer> tempIds = entry.getValue();
                if (tempIds!=null && tempIds.size() > 0) {
                    for (Integer temp : tempIds) {
                        uIds.add(temp);
                    }
                }
            }
        }else if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_PERSON)) {
            for (TbGameEventScore tbGameEventScore : tbGameEventScores) {
                uIds.add(tbGameEventScore.getUid());
            }

        }
        logger.info("赛事结束,开始计算栏目积分, 当前赛事:{}, 参赛队伍大小:{}", tbGameEvent.getId(), uIds.size());

        //获取用户的信息
        Map<Integer, RespUserInfoBase> userMsgMap = baseService.queryUserMsg(new ArrayList<Integer>(uIds));
        logger.info("赛事结束,开始计算栏目积分, 当前赛事:{}, 查询出的用户大小:{}", tbGameEvent.getId(), userMsgMap.size());

        //积分录入
        for (TbGameEventScore tbGameEventScore:tbGameEventScores) {
            TbPoints tbPoints = new TbPoints();
            tbPoints.setCategoryId(tbGameEvent.getCategoryId());
            if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                List<Integer> tempUid = clubUidMap.get(tbGameEventScore.getClubId());
                if (null!=tempUid &&  tempUid.size() > 0) {
                    tbPoints.setScore(tbGameEventScore.getScore().intValue());
                    for (Integer temp : tempUid) {
                        tbPoints.setUid(temp);
                        if (null!=userMsgMap && null!=userMsgMap.get(tbGameEventScore.getUid())) {
                            tbPoints.setCityId(userMsgMap.get(temp).getAreaId());
                        }
                        pointsService.entryUserPoints(tbPoints);
                    }
                }
            }else {
                tbPoints.setScore(tbGameEventScore.getScore().intValue());
                tbPoints.setUid(tbGameEventScore.getUid());
                if (null!=userMsgMap && null!=userMsgMap.get(tbGameEventScore.getUid())) {
                    tbPoints.setCityId(userMsgMap.get(tbGameEventScore.getUid()).getAreaId());
                }
            }
            pointsService.entryUserPoints(tbPoints);
        }
        logger.info("赛事结束,开始计算栏目积分, 当前赛事:{}, 积分录入完毕:{}", tbGameEvent.getId());
        //赛事排名推送
        /*Set<Integer> clubIs = new HashSet<>();
        Set<Integer> uidIds = new HashSet<>();
        List<VDefinedGameSchedule> gameScheduleMsg = bizGameSystemDao.selectVDefinedGameScheduleByGameRoundId(tbGameRound.getId(), null, null);
        Map<Integer, List<VDefinedGameSchedule>> vMap = new HashMap<>();
        for (VDefinedGameSchedule vDefinedGameSchedule : gameScheduleMsg) {   //数据整理
            clubIs.add(vDefinedGameSchedule.getClubId());
            uidIds.add(vDefinedGameSchedule.getUid());
            List<VDefinedGameSchedule> temp = vMap.get(vDefinedGameSchedule.getGameScheduleId());
            if (null==temp || temp.size()<1){
                temp = new ArrayList<>();
            }
            temp.add(vDefinedGameSchedule);
            vMap.put(vDefinedGameSchedule.getGameScheduleId(), temp);
        }

        Map<Integer, List<Integer>> clubMasterUids = new HashMap<>();
        if (tbGameEvent.getGameTeamType() == GameConstants.TEAM_TYPE_CLUB) {
            clubMasterUids= baseService.queryClubIds(new ArrayList<Integer>(clubIs));
            for (Map.Entry<Integer, List<Integer>> entry : clubMasterUids.entrySet()) {
                if (null!=entry.getValue() && entry.getValue().size()>0) {
                    uidIds.addAll(entry.getValue());
                }
            }
        }
        Map<Integer, TbPoints> uidCategoryScore = this.batchQueryUserCategoryScore(new ArrayList<Integer>(uidIds), tbGameEvent.getCategoryId());
        logger.info("开始进行栏目积分的插入, 当前赛事:{}, 当前轮次:{}", tbGameEvent.getId(), tbGameRound.getId());
        this.gameScheduleCategoryScoreRecordSon(tbGameEvent, vMap, clubMasterUids, uidCategoryScore);
        logger.info("结束更新用户栏目积分, 当前赛事:{}, 当前轮次:{}", tbGameEvent.getId(), tbGameRound.getId());*/
    }

    @Override
    public void gameScheduleCategoryScoreRecordSon(TbGameEvent tbGameEvent, Map<Integer, List<VDefinedGameSchedule>> vMap,
                                                   Map<Integer, List<Integer>> clubMasterUids, Map<Integer, TbPoints> uidCategoryScore) {
        for (Map.Entry<Integer, List<VDefinedGameSchedule>> entry : vMap.entrySet()) {  //分数统计
            List<VDefinedGameSchedule> vDefinedGameSchedules = entry.getValue();
            List<Integer> homeUid = new ArrayList<>();
            List<Integer> awayUid = new ArrayList<>();
            if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_PERSON)) {  //个人
                for (VDefinedGameSchedule vDefinedGameSchedule : entry.getValue()) {
                    if (vDefinedGameSchedule.getType().equals(GameConstants.IS_NOT_DELETE)) {  //主场
                        homeUid.add(vDefinedGameSchedule.getUid());
                    } else if (vDefinedGameSchedule.getType().equals(GameConstants.IS_DELETE)) {  //客场
                        awayUid.add(vDefinedGameSchedule.getUid());
                    } else {
                    }
                }
            }else {  //俱乐部
                for (VDefinedGameSchedule vDefinedGameSchedule : entry.getValue()) {
                    if (vDefinedGameSchedule.getType().equals(GameConstants.IS_NOT_DELETE)) {  //主场
                        if (null!=clubMasterUids.get(vDefinedGameSchedule.getClubId()) && clubMasterUids.get(vDefinedGameSchedule.getClubId()).size()>0) {
                            homeUid.addAll(clubMasterUids.get(vDefinedGameSchedule.getClubId()));
                        }
                    } else if (vDefinedGameSchedule.getType().equals(GameConstants.IS_DELETE)) {  //客场
                        if (null!=clubMasterUids.get(vDefinedGameSchedule.getClubId()) && clubMasterUids.get(vDefinedGameSchedule.getClubId()).size()>0) {
                            awayUid.addAll(clubMasterUids.get(vDefinedGameSchedule.getClubId()));
                        }
                    } else {
                    }
                }
            }
            logger.info("栏目积分的录入,开始进行积分编辑, 当前赛事:{}", tbGameEvent.getId());
            VDefinedGameSchedule schedule = vDefinedGameSchedules.get(0);
            List<Integer> fromScore = new ArrayList<>();
            List<Integer> toScore = new ArrayList<>();
            for (int i=0;i<homeUid.size();i++) {
                fromScore.add(0);
                toScore.add(0);
                if (null!=uidCategoryScore && uidCategoryScore.size()>0) {
                    if (null!=uidCategoryScore.get(homeUid.get(i)) && uidCategoryScore.get(homeUid.get(i)).getId()>0) {
                        fromScore.add(uidCategoryScore.get(homeUid.get(i)).getScore());
                    }
                    if (null!=awayUid && awayUid.size() >0 && i<awayUid.size() && null!=uidCategoryScore.get(awayUid.get(i)) && uidCategoryScore.get(awayUid.get(i)).getId()>0) {
                        toScore.add(uidCategoryScore.get(homeUid.get(i)).getScore());
                    }
                }
            }
            logger.info("栏目积分的录入,积分编辑编辑完毕, 当前赛事:{}", tbGameEvent.getId());
            for (int i=0;i<homeUid.size();i++) {
                if (schedule.getIsWin().intValue() == GameConstants.SCHEDULE_WIN.intValue()) {
                    this.modifyUserScore(homeUid.get(i), this.integralUserPkScore(fromScore, toScore, GameConstants.SCHEDULE_WIN.intValue()), tbGameEvent.getCategoryId());
                    if (awayUid.size()>0){
                        this.modifyUserScore(awayUid.get(i), this.integralUserPkScore(fromScore, toScore, GameConstants.SCHEDULE_LOSE.intValue()), tbGameEvent.getCategoryId());
                    }
                }else if (schedule.getIsWin().intValue() == GameConstants.SCHEDULE_LOSE.intValue()) {
                    this.modifyUserScore(homeUid.get(i), this.integralUserPkScore(fromScore, toScore, GameConstants.SCHEDULE_LOSE.intValue()), tbGameEvent.getCategoryId());
                    if (awayUid.size()>0){
                        this.modifyUserScore(awayUid.get(i), this.integralUserPkScore(fromScore, toScore, GameConstants.SCHEDULE_WIN.intValue()), tbGameEvent.getCategoryId());
                    }
                }else {
                    this.modifyUserScore(homeUid.get(i), this.integralUserPkScore(fromScore, toScore, GameConstants.SCHEDULE_TIE.intValue()), tbGameEvent.getCategoryId());
                    if (awayUid.size()>0){
                        this.modifyUserScore(awayUid.get(i), this.integralUserPkScore(fromScore, toScore, GameConstants.SCHEDULE_TIE.intValue()), tbGameEvent.getCategoryId());
                    }
                }
            }
            logger.info("栏目积分的录入,积分录入完毕, 当前赛事:{}", tbGameEvent.getId());
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout=10, rollbackFor = Exception.class)
    public void userGameScheduleInit(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        TbGameRound tbGameRound = stadiumNotifyMsg.getCurrentGameRound();
        if (tbGameEvent == null || tbGameEvent.getId()<1 || tbGameRound == null || tbGameRound.getId() < 1) {
            return;
        }

        Map<Integer, List<Integer>> mapClub = new HashMap<>();    //俱乐部下面的信息(俱乐部代表队在赛事一旦创建的时候固定)
        if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {
            List<TbGameClubTeamUser> teamUsers = bizGameSystemDao.queryTbGameClubTeamUserByClubId(tbGameEvent.getId(), null);
            List<Integer> ids = new ArrayList<>();
            for (TbGameClubTeamUser user : teamUsers) {
                ids = mapClub.get(user.getClubId());
                if (null == ids || ids.size() < 1) {
                    ids = new ArrayList<>();
                }
                ids.add(user.getUid());
                mapClub.put(user.getClubId(), ids);
            }
        }
        List<VDefinedGameSchedule> vDefinedGameSchedules = bizGameSystemDao.selectVDefinedGameScheduleByGameRoundId(tbGameRound.getId(), null, null);
        if (null == vDefinedGameSchedules || vDefinedGameSchedules.size() < 1) {
            return;
        }
        Map<Integer, Set<VDefinedGameSchedule>> mapSchedules = new HashMap<>();
        Set<VDefinedGameSchedule> setSchedule = new HashSet<>();
        for (VDefinedGameSchedule vDefinedGameSchedule : vDefinedGameSchedules) {
            setSchedule = mapSchedules.get(vDefinedGameSchedule.getGameScheduleId());
            if (null == setSchedule || setSchedule.size() < 1) {
                setSchedule = new HashSet<>();
            }
            setSchedule.add(vDefinedGameSchedule);
            mapSchedules.put(vDefinedGameSchedule.getGameScheduleId(), setSchedule);
        }
        logger.info("开始插入用户赛程表信息, 当前赛事:{}, 当前轮次:{}", tbGameEvent.getId(), tbGameRound.getId());
        this.userGameScheduleInitSon(mapSchedules, tbGameEvent, mapClub);
        logger.info("初始化用户赛程信息, 当前赛事:{}, 当前轮次:{}", tbGameEvent.getId(), tbGameRound.getId());
    }

    @Override
    public void userGameScheduleInitSon(Map<Integer, Set<VDefinedGameSchedule>> mapSchedules, TbGameEvent tbGameEvent,
                                        Map<Integer, List<Integer>> mapClub) {
        for (Map.Entry<Integer, Set<VDefinedGameSchedule>> entry : mapSchedules.entrySet()) {
            TbUserGameSchedule schedule = new TbUserGameSchedule();
            schedule.setGameEventType(tbGameEvent.getGameEventType());
            schedule.setGameName(tbGameEvent.getName());
            schedule.setType(tbGameEvent.getGameTeamType().intValue());
            schedule.setGameEventId(tbGameEvent.getId());
            schedule.setPlaceId(tbGameEvent.getPlaceId());
            schedule.setGameCategoryId(tbGameEvent.getCategoryId());
            List<Integer> homeUid = new ArrayList<>();
            List<Integer> awayUid = new ArrayList<>();
            List<Integer> homeClubId = new ArrayList<>();
            List<Integer> awayClubId = new ArrayList<>();
            for (VDefinedGameSchedule vDefinedGameSchedule : entry.getValue()) {
                if (vDefinedGameSchedule.getType().equals(GameConstants.IS_NOT_DELETE)) {  //主场
                    homeUid.add(vDefinedGameSchedule.getUid());
                    homeClubId.add(vDefinedGameSchedule.getClubId());
                } else if (vDefinedGameSchedule.getType().equals(GameConstants.IS_DELETE)) {  //客场
                    awayUid.add(vDefinedGameSchedule.getUid());
                    awayClubId.add(vDefinedGameSchedule.getClubId());
                } else {
                }
                schedule.setGameScheduleId(entry.getKey());
                if (vDefinedGameSchedule.getStartTime()!=null) {
                    schedule.setStartTime(vDefinedGameSchedule.getStartTime());
                }
                if (vDefinedGameSchedule.getEndTime()!=null) {
                    schedule.setEndTime(vDefinedGameSchedule.getEndTime());
                }
                schedule.setScheduleStatus(vDefinedGameSchedule.getIsEnd());
            }

            if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {
                schedule.setType(1);
                schedule.setHomePlayerList(StringUtils.join(homeClubId.toArray(), ","));
                schedule.setAwayPlayerList(StringUtils.join(awayClubId.toArray(), ","));
                for (Integer temp : homeClubId) {
                    if (null!=mapClub && mapClub.size()>0) {
                        List<Integer> uids =mapClub.get(temp);
                        if (null!=uids && uids.size()>0) {
                            for (Integer integer1 : uids) {
                                schedule.setClubId(temp);
                                schedule.setUid(integer1);
                                tbUserGameScheduleMapper.insertSelective(schedule);
                            }
                        }
                    }
                }
                for (Integer temp1 : awayClubId) {
                    if (null!=mapClub && mapClub.size()>0) {
                        List<Integer> uids =mapClub.get(temp1);
                        if (null!=uids && uids.size()>0) {
                            for (Integer integer2 : uids) {
                                schedule.setClubId(temp1);
                                schedule.setUid(integer2);
                                tbUserGameScheduleMapper.insertSelective(schedule);
                            }
                        }
                    }
                }
            }else if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_PERSON.byteValue())) {
                schedule.setHomePlayerList(StringUtils.join(homeUid.toArray(), ","));
                schedule.setAwayPlayerList(StringUtils.join(awayUid.toArray(), ","));
                for (Integer temp2 : homeUid) {
                    schedule.setUid(temp2);
                    tbUserGameScheduleMapper.insertSelective(schedule);
                }
                for (Integer temp3 : awayUid) {
                    schedule.setUid(temp3);
                    tbUserGameScheduleMapper.insertSelective(schedule);
                }
            }else {
            }
        }
    }

    @Override
    public void userGameScheduleUpdate(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameSchedule tbGameSchedule = stadiumNotifyMsg.getTbGameSchedule();
        if (tbGameSchedule == null || tbGameSchedule.getId()<1) {
            return;
        }
        TbGameSchedule schedule = tbGameScheduleMapper.selectByPrimaryKey(tbGameSchedule.getId());
        TbUserGameScheduleExample example = new TbUserGameScheduleExample();
        TbUserGameScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andGameScheduleIdEqualTo(schedule.getId());

        TbUserGameSchedule userGameSchedule = new TbUserGameSchedule();
        userGameSchedule.setStartTime(schedule.getStartTime());
        userGameSchedule.setEndTime(schedule.getEndTime());
        userGameSchedule.setPlaceUnitId(schedule.getPlaceUnitId());
        tbUserGameScheduleMapper.updateByExampleSelective(userGameSchedule, example);
        logger.info("更新用户赛程信息, 当前赛程:{}", schedule.getId());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout=10, rollbackFor = Exception.class)
    public void userGameScheduleFinally(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        TbGameRound tbGameRound = stadiumNotifyMsg.getCurrentGameRound();
        if (tbGameEvent == null || tbGameEvent.getId() < 1 || tbGameRound == null || tbGameRound.getId() < 1) {
            return;
        }

        Map<Integer, List<Integer>> mapClub = new HashMap<>();    //俱乐部下面的信息
        if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {
            List<TbGameEventTeam> tbGameEventTeamList = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getId(), GameConstants.IS_NOT_DELETE);
            List<Integer> ids = new ArrayList<>();
            for (TbGameEventTeam team : tbGameEventTeamList) {
                ids.add(team.getClubId());
            }
            mapClub = baseService.queryClubIds(ids);
        }

        List<VDefinedGameSchedule> vDefinedGameSchedules = bizGameSystemDao.selectVDefinedGameScheduleByGameRoundId(tbGameRound.getId(), null, null);
        if (null == vDefinedGameSchedules || vDefinedGameSchedules.size() < 1) {
            return;
        }
        Map<Integer, Set<VDefinedGameSchedule>> mapSchedules = new HashMap<>();
        Set<VDefinedGameSchedule> setSchedule = new HashSet<>();
        for (VDefinedGameSchedule vDefinedGameSchedule : vDefinedGameSchedules) {
            setSchedule = mapSchedules.get(vDefinedGameSchedule.getGameScheduleId());
            if (null == setSchedule || setSchedule.size() < 1) {
                setSchedule = new HashSet<>();
            }
            setSchedule.add(vDefinedGameSchedule);
            mapSchedules.put(vDefinedGameSchedule.getGameScheduleId(), setSchedule);
        }

        if (null == vDefinedGameSchedules || vDefinedGameSchedules.size() < 1) {
            return;
        }
        logger.info("用户赛程表数据的最终插入, 当前赛事:{}, 当前轮次:{}", tbGameEvent.getId(), tbGameRound.getId());
        this.userGameScheduleFinallySon(mapSchedules, mapClub, tbGameEvent);
        logger.info("最终更新用户赛程信息, 当前赛事:{}, 当前轮次:{}", tbGameEvent.getId(), tbGameRound.getId());
    }

    @Override
    public void userGameScheduleFinallySon(Map<Integer, Set<VDefinedGameSchedule>> mapSchedules, Map<Integer, List<Integer>> mapClub,
                                           TbGameEvent tbGameEvent) {
        for (Map.Entry<Integer, Set<VDefinedGameSchedule>> entry : mapSchedules.entrySet()) {
            if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {
                for (VDefinedGameSchedule vDefinedGameSchedule : entry.getValue()) {
                    TbUserGameSchedule schedule = new TbUserGameSchedule();
                    TbUserGameScheduleExample example1 = new TbUserGameScheduleExample();
                    TbUserGameScheduleExample.Criteria criteria1 = example1.createCriteria();
                    criteria1.andGameScheduleIdEqualTo(entry.getKey());
                    schedule.setScheduleScore(vDefinedGameSchedule.getFinalScore());
                    schedule.setScheduleStatus(GameConstants.IS_DELETE);
                    schedule.setIsWin(vDefinedGameSchedule.getIsWin());
                    schedule.setStartTime(vDefinedGameSchedule.getStartTime());
                    schedule.setEndTime(vDefinedGameSchedule.getEndTime());
                    if (vDefinedGameSchedule.getType().equals(GameConstants.IS_DELETE)) {  //客场
                        if (vDefinedGameSchedule.getIsWin().equals(GameConstants.IS_DELETE)) {
                            schedule.setIsWin(GameConstants.IS_NOT_DELETE);
                        } else if (vDefinedGameSchedule.getIsWin().equals(GameConstants.IS_NOT_DELETE)) {
                            schedule.setIsWin(GameConstants.IS_DELETE);
                        } else {
                            ;
                        }
                        if (null != mapClub && mapClub.size() > 0) {        //更新俱乐部下的每一个用户
                            List<Integer> uids = mapClub.get(vDefinedGameSchedule.getClubId());
                            if (null != uids && uids.size() > 0) {
                                for (Integer integer1 : uids) {
                                    criteria1.andUidEqualTo(integer1);
                                    criteria1.andClubIdEqualTo(vDefinedGameSchedule.getClubId());
                                    tbUserGameScheduleMapper.updateByExampleSelective(schedule, example1);
                                }
                            }
                        }
                    }
                }
            } else if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_PERSON.byteValue())) {
                for (VDefinedGameSchedule vDefinedGameSchedule : entry.getValue()) {
                    TbUserGameSchedule schedule = new TbUserGameSchedule();
                    TbUserGameScheduleExample example1 = new TbUserGameScheduleExample();
                    TbUserGameScheduleExample.Criteria criteria1 = example1.createCriteria();
                    criteria1.andGameScheduleIdEqualTo(entry.getKey());
                    criteria1.andUidEqualTo(vDefinedGameSchedule.getUid());
                    schedule.setScheduleScore(vDefinedGameSchedule.getFinalScore());
                    schedule.setScheduleStatus(GameConstants.IS_DELETE);
                    schedule.setIsWin(vDefinedGameSchedule.getIsWin());
                    schedule.setStartTime(vDefinedGameSchedule.getStartTime());
                    schedule.setEndTime(vDefinedGameSchedule.getEndTime());
                    if (vDefinedGameSchedule.getType().equals(GameConstants.IS_DELETE)) {  //客场
                        if (vDefinedGameSchedule.getIsWin().equals(GameConstants.IS_DELETE)) {
                            schedule.setIsWin(GameConstants.IS_NOT_DELETE);
                        } else if (vDefinedGameSchedule.getIsWin().equals(GameConstants.IS_NOT_DELETE)) {
                            schedule.setIsWin(GameConstants.IS_DELETE);
                        } else {
                            ;
                        }
                    }
                    tbUserGameScheduleMapper.updateByExampleSelective(schedule, example1);
                }
            } else {
                ;
            }
        }
    }

    @Override
    public void userGameEventCollect(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        if (tbGameEvent == null || tbGameEvent.getId()<1) {
            return;
        }

        logger.info("赛事结束,调起userGameEventCollect方法进行赛事积分,以及相应的计算,消息的推送,当前赛事id:{}", tbGameEvent.getId());
        gameScoreService.gameTeamRankList(tbGameEvent);
    }


    private void officialGameEventClubJoin(TbGameEvent tbGameEvent){
        List<TbGameEventTeam> tbGameEventTeamList = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getId(), GameConstants.IS_NOT_DELETE);
        if (null!=tbGameEventTeamList && tbGameEventTeamList.size()>0) {
            Set<Integer> clubIds = new HashSet<>();
            for (TbGameEventTeam temp : tbGameEventTeamList) {
                clubIds.add(temp.getClubId());
            }
            gameMQProduct.clubJoinOfficialGameNumClubMedalMQ(tbGameEvent.getCategoryId(), new ArrayList<Integer>(clubIds));
        }
    }


    private void gameEventFinishSetClubTop(List<TbGameEventScore> tbGameEventScores, TbGameEvent tbGameEvent,
                                           Set<Integer> uIds){

        List<Integer> clubIds = new ArrayList<>();
        for (TbGameEventScore tbGameEventScore : tbGameEventScores) {  //获取俱乐部
            clubIds.add(tbGameEventScore.getClubId());
        }
        Map<Integer, List<Integer>> clubUids = baseService.queryClubIds(clubIds);  //获取俱乐部代表队
        if (clubUids == null || clubUids.size()<1) {
            return;
        }
        for (Map.Entry<Integer,  List<Integer>> entry : clubUids.entrySet()) {
            List<Integer> tempIds = entry.getValue();
            if (tempIds!=null && tempIds.size() > 0) {
                for (Integer temp : tempIds) {
                    uIds.add(temp);
                }
            }
        }
        TbUserGameCollectExample example1 = new TbUserGameCollectExample();
        TbUserGameCollectExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUidIn(new ArrayList<Integer>(uIds));
        criteria1.andGameCategoryIdEqualTo(tbGameEvent.getCategoryId());
        List<TbUserGameCollect> collects = tbUserGameCollectMapper.selectByExample(example1);
        if (null==collects || collects.size()<1) {
            return;
        }
        Map<Integer, TbUserGameCollect> mapTemp = new HashMap<>();
        for (TbUserGameCollect tbUserGameCollect : collects) {
            mapTemp.put(tbUserGameCollect.getUid(), tbUserGameCollect);
        }
        for (TbGameEventScore tbGameEventScore : tbGameEventScores) {
            List<Integer> userIds = clubUids.get(tbGameEventScore.getClubId());
            if (null == userIds || userIds.size() < 1) {
                break;
            }
            for (Integer tmp : userIds) {
                TbUserGameCollect collect = new TbUserGameCollect();
                if (mapTemp!=null && mapTemp.get(tbGameEventScore.getUid()) != null && mapTemp.get(tbGameEventScore.getUid()).getId() > 0) {
                    collect.setId(mapTemp.get(tbGameEventScore.getUid()).getId());
                    collect.setGameScheduleNum(tbGameEventScore.getProducts().intValue() + mapTemp.get(tbGameEventScore.getUid()).getGameScheduleNum().intValue());
                    collect.setWinGameScheduleNum(tbGameEventScore.getWins().intValue() + mapTemp.get(tbGameEventScore.getUid()).getWinGameScheduleNum().intValue());
                    if (tbGameEventScore.getRank()!=null) {
                        if(tbGameEventScore.getRank().intValue() == 1) {
                            collect.setTopFirstNum(1);;
                        }else if (tbGameEventScore.getRank().intValue()>1 && tbGameEventScore.getRank().intValue()<=4) {
                            collect.setTopFirstNum(1);
                            collect.setTopFourNum(1);
                        }else if (tbGameEventScore.getRank().intValue()>4 && tbGameEventScore.getRank().intValue()<=8) {
                            collect.setTopFirstNum(1);
                            collect.setTopFourNum(1);
                            collect.setTopEightNum(1);
                        }else if (tbGameEventScore.getRank().intValue()>8 && tbGameEventScore.getRank().intValue()<=16) {
                            collect.setTopFirstNum(1);
                            collect.setTopFourNum(1);
                            collect.setTopEightNum(1);
                            collect.setTopSixteenNum(1);
                        }else {
                            ;
                        }
                    }
                    tbUserGameCollectMapper.updateByPrimaryKeySelective(collect);
                }else {
                    collect.setGameType(GameConstants.GAME_TYPE_OFFICIAL);
                    collect.setGameCategoryId(tbGameEvent.getCategoryId());
                    collect.setGameScheduleNum(tbGameEventScore.getProducts());
                    collect.setWinGameScheduleNum(tbGameEventScore.getWins());
                    collect.setUid(tbGameEventScore.getUid());
                    if (tbGameEventScore.getRank()!=null) {
                        if(tbGameEventScore.getRank().intValue() == 1) {
                            collect.setTopFirstNum(1);;
                        }else if (tbGameEventScore.getRank().intValue()>1 && tbGameEventScore.getRank().intValue()<=4) {
                            collect.setTopFirstNum(1);
                            collect.setTopFourNum(1);
                        }else if (tbGameEventScore.getRank().intValue()>4 && tbGameEventScore.getRank().intValue()<=8) {
                            collect.setTopFirstNum(1);
                            collect.setTopFourNum(1);
                            collect.setTopEightNum(1);
                        }else if (tbGameEventScore.getRank().intValue()>8 && tbGameEventScore.getRank().intValue()<=16) {
                            collect.setTopFirstNum(1);
                            collect.setTopFourNum(1);
                            collect.setTopEightNum(1);
                            collect.setTopSixteenNum(1);
                        }else {
                            ;
                        }
                    }
                    tbUserGameCollectMapper.updateByPrimaryKeySelective(collect);
                }
            }
        }
    }


    public void personGameEventFinishSetPersonTop(TbGameEvent tbGameEvent, Set<Integer> uIds){
        List<TbGameRound> temp1 = bizGameSystemDao.selectTbGameRoundByGameEventId(tbGameEvent.getId());
        List<TbGameSchedule> temp2 = bizGameSystemDao.selectTbGameScheduleByGameRoundId(temp1.get(0).getId(), 0, GameConstants.DEFAULT_PLACE_UNIT_NUM);
        List<VDefinedGameSchedule> vDefinedGameSchedules = bizGameSystemDao.selectVDefinedGameScheduleById(temp2.get(0).getId());

        for (VDefinedGameSchedule ve : vDefinedGameSchedules) {
            uIds.add(ve.getUid());
        }
        TbUserGameCollectExample example1 = new TbUserGameCollectExample();
        TbUserGameCollectExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUidIn(new ArrayList<Integer>(uIds));
        criteria1.andGameCategoryIdEqualTo(tbGameEvent.getCategoryId());
        List<TbUserGameCollect> collects = tbUserGameCollectMapper.selectByExample(example1);
        Map<Integer, TbUserGameCollect> mapTemp = new HashMap<>();

        if (null!=collects && collects.size()>0) {
            for (TbUserGameCollect tbUserGameCollect : collects) {
                mapTemp.put(tbUserGameCollect.getUid(), tbUserGameCollect);
            }
        }
        for (VDefinedGameSchedule ve : vDefinedGameSchedules) {
            TbUserGameCollect collect = new TbUserGameCollect();
            if (mapTemp!=null && mapTemp.get(ve.getUid()) != null && mapTemp.get(ve.getUid()).getId() > 0) {
                collect.setId(mapTemp.get(ve.getUid()).getId());
                collect.setGameScheduleNum(1+mapTemp.get(ve.getUid()).getGameScheduleNum().intValue());
                if (ve.getIsWin().intValue() == GameConstants.SCHEDULE_WIN.intValue() && ve.getType().intValue() == GameConstants.IS_NOT_DELETE.intValue()) {
                    collect.setWinGameScheduleNum(1 + mapTemp.get(ve.getUid()).getWinGameScheduleNum().intValue());
                }else if (ve.getIsWin().intValue() == GameConstants.SCHEDULE_LOSE.intValue() && ve.getType().intValue() == GameConstants.IS_DELETE.intValue()) {
                    collect.setWinGameScheduleNum(1 + mapTemp.get(ve.getUid()).getWinGameScheduleNum().intValue());
                }
                tbUserGameCollectMapper.updateByPrimaryKeySelective(collect);
            }else {
                collect.setGameType(GameConstants.GAME_TYPE_OFFICIAL);
                collect.setGameCategoryId(tbGameEvent.getCategoryId());
                collect.setGameScheduleNum(1);
                collect.setWinGameScheduleNum(ve.getIsWin()>0?1:0);
                collect.setUid(ve.getUid());
                tbUserGameCollectMapper.insertSelective(collect);
            }
        }
    }

    private void gameEventFinishSetPersonTop(List<TbGameEventScore> tbGameEventScores, TbGameEvent tbGameEvent,
                                             Set<Integer> uIds){

        for (TbGameEventScore tbGameEventScore : tbGameEventScores) {
            uIds.add(tbGameEventScore.getUid());
        }
        TbUserGameCollectExample example1 = new TbUserGameCollectExample();
        TbUserGameCollectExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUidIn(new ArrayList<Integer>(uIds));
        criteria1.andGameCategoryIdEqualTo(tbGameEvent.getCategoryId());
        List<TbUserGameCollect> collects = tbUserGameCollectMapper.selectByExample(example1);
        Map<Integer, TbUserGameCollect> mapTemp = new HashMap<>();

        if (null!=collects && collects.size()>0) {
            for (TbUserGameCollect tbUserGameCollect : collects) {
                mapTemp.put(tbUserGameCollect.getUid(), tbUserGameCollect);
            }
        }
        for (TbGameEventScore tbGameEventScore : tbGameEventScores) {
            TbUserGameCollect collect = new TbUserGameCollect();
            if (mapTemp!=null && mapTemp.get(tbGameEventScore.getUid()) != null && mapTemp.get(tbGameEventScore.getUid()).getId() > 0) {
                collect.setId(mapTemp.get(tbGameEventScore.getUid()).getId());
                collect.setGameScheduleNum(tbGameEventScore.getProducts().intValue() + mapTemp.get(tbGameEventScore.getUid()).getGameScheduleNum().intValue());
                collect.setWinGameScheduleNum(tbGameEventScore.getWins().intValue() + mapTemp.get(tbGameEventScore.getUid()).getWinGameScheduleNum().intValue());
                if (tbGameEventScore.getRank()!=null) {
                    if(tbGameEventScore.getRank().intValue() == 1) {
                        collect.setTopFirstNum(1 + mapTemp.get(tbGameEventScore.getUid()).getTopFirstNum().intValue());;
                    }else if (tbGameEventScore.getRank().intValue()>1 && tbGameEventScore.getRank().intValue() <= 4) {
                        collect.setTopFirstNum(1 + mapTemp.get(tbGameEventScore.getUid()).getTopFirstNum().intValue());
                        collect.setTopFourNum(1 + mapTemp.get(tbGameEventScore.getUid()).getTopFirstNum().intValue());
                    }else if (tbGameEventScore.getRank().intValue()>4 && tbGameEventScore.getRank().intValue()<=8) {
                        collect.setTopFirstNum(1 + mapTemp.get(tbGameEventScore.getUid()).getTopFirstNum().intValue());
                        collect.setTopFourNum(1 + mapTemp.get(tbGameEventScore.getUid()).getTopFirstNum().intValue());
                        collect.setTopEightNum(1);
                    }else if (tbGameEventScore.getRank().intValue()>8 && tbGameEventScore.getRank().intValue()<=16) {
                        collect.setTopFirstNum(1 + mapTemp.get(tbGameEventScore.getUid()).getTopFirstNum().intValue());
                        collect.setTopFourNum(1 + mapTemp.get(tbGameEventScore.getUid()).getTopFirstNum().intValue());
                        collect.setTopEightNum(1 + mapTemp.get(tbGameEventScore.getUid()).getTopFirstNum().intValue());
                        collect.setTopSixteenNum(1 + mapTemp.get(tbGameEventScore.getUid()).getTopFirstNum().intValue());
                    }else {
                        ;
                    }
                }
                tbUserGameCollectMapper.updateByPrimaryKeySelective(collect);
            }else {
                collect.setGameType(GameConstants.GAME_TYPE_OFFICIAL);
                collect.setGameCategoryId(tbGameEvent.getCategoryId());
                collect.setGameScheduleNum(tbGameEventScore.getProducts());
                collect.setWinGameScheduleNum(tbGameEventScore.getWins());
                collect.setUid(tbGameEventScore.getUid());
                if (tbGameEventScore.getRank()!=null) {
                    if(tbGameEventScore.getRank().intValue() == 1) {
                        collect.setTopFirstNum(1);;
                    }else if (tbGameEventScore.getRank().intValue()>1 && tbGameEventScore.getRank().intValue()<=4) {
                        collect.setTopFirstNum(1);
                        collect.setTopFourNum(1);
                    }else if (tbGameEventScore.getRank().intValue()>4 && tbGameEventScore.getRank().intValue()<=8) {
                        collect.setTopFirstNum(1);
                        collect.setTopFourNum(1);
                        collect.setTopEightNum(1);
                    }else if (tbGameEventScore.getRank().intValue()>8 && tbGameEventScore.getRank().intValue()<=16) {
                        collect.setTopFirstNum(1);
                        collect.setTopFourNum(1);
                        collect.setTopEightNum(1);
                        collect.setTopSixteenNum(1);
                    }else {
                        ;
                    }
                }
                tbUserGameCollectMapper.insertSelective(collect);
            }
        }
    }


    @Override
    public void userPKGameEventCollect(StadiumNotifyMsg stadiumNotifyMsg) {
        Integer fromUid = stadiumNotifyMsg.getFromUid();
        Integer toUid = stadiumNotifyMsg.getToUid();
        Integer categoryId = stadiumNotifyMsg.getCategoryId();
        Integer pkResult = stadiumNotifyMsg.getPkResult();
        if (null == fromUid || null == toUid || null == categoryId || null==pkResult) {
            return;
        }
        List<Integer> uids = new ArrayList<>();
        uids.add(fromUid);
        uids.add(toUid);

        TbUserGameCollectExample example1 = new TbUserGameCollectExample();
        TbUserGameCollectExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUidIn(uids);
        criteria1.andGameCategoryIdEqualTo(categoryId);
        List<TbUserGameCollect> collects = tbUserGameCollectMapper.selectByExample(example1);
        Map<Integer, TbUserGameCollect> mapTemp = new HashMap<>();
        for (TbUserGameCollect tbUserGameCollect : collects) {
            mapTemp.put(tbUserGameCollect.getUid(), tbUserGameCollect);
        }


        for (Integer inte : uids) {
            TbUserGameCollect collect = new TbUserGameCollect();
            if (null!=mapTemp && mapTemp.size() >0 && mapTemp.get(inte).getUid().intValue() == inte.intValue()) {
                if (GameConstants.PK_RESULT_WIN==pkResult.intValue() && inte.intValue() == fromUid.intValue()) {  //1认为主场胜利
                    collect.setWinGameScheduleNum(collect.getWinGameScheduleNum() + 1);
                }else if (GameConstants.PK_RESULT_LOSE==pkResult.intValue() && inte.intValue() == toUid.intValue()){   //2认为客场胜利
                    collect.setWinGameScheduleNum(collect.getWinGameScheduleNum() + 1);
                }
                collect.setGameScheduleNum(collect.getGameScheduleNum() + 1);
                tbUserGameCollectMapper.updateByPrimaryKeySelective(collect);
            }else {
                collect.setGameType(GameConstants.GAME_TYPE_PK);
                collect.setGameCategoryId(categoryId);
                collect.setGameScheduleNum(1);
                if (GameConstants.PK_RESULT_WIN==pkResult.intValue() && inte.intValue() == fromUid.intValue()) {
                    collect.setWinGameScheduleNum(1);
                }else if (GameConstants.PK_RESULT_LOSE==pkResult.intValue() && inte.intValue() == toUid.intValue()){
                    collect.setWinGameScheduleNum(1);
                }
                collect.setUid(inte);
                tbUserGameCollectMapper.insertSelective(collect);
            }
        }
        logger.info("PK赛事完结,记录用户的赛事数目以及赛程数目");
    }

    @Override
    public void generateNextPagePKRankScore(StadiumNotifyMsg stadiumNotifyMsg) {
        //设置锁
        Integer cityId = stadiumNotifyMsg.getCityId();
        Integer categoryId = stadiumNotifyMsg.getCategoryId();
        Integer curNum = stadiumNotifyMsg.getCurNum();
        Integer totalNum = stadiumNotifyMsg.getTotalNum();
        if (categoryId == null || categoryId.intValue() == 0) {
            return;
        }
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, "PK_RANK_"+categoryId+cityId);
        long flag = myRedisTemplate.setnx(redisKey);
        if (flag == 0) {
            return;
        }
        try {
            TbUserScoreCollectExample example = new TbUserScoreCollectExample();
            TbUserScoreCollectExample.Criteria cr = example.createCriteria();
            cr.andCategoryIdEqualTo(categoryId);
            if (cityId!=null && cityId.intValue() != 0) {
                cr.andCityIdEqualTo(cityId);
            }
            example.setOrderByClause("score desc, update_time desc");
            example.setPage(new Page(curNum, Constants.DEFAULT_PAGE_SIZE));
            if (null == totalNum || totalNum == 0) {
                example.setPage(new Page(0, Constants.DEFAULT_PAGE_SIZE));
            }
            List<TbUserScoreCollect> rs = tbUserScoreCollectMapper.selectByExample(example);
            if (null!=rs && rs.size() > 0) {
                if(null == totalNum || totalNum == 0){
                    TbUserScoreCollectExample exampleTemp = new TbUserScoreCollectExample();
                    TbUserScoreCollectExample.Criteria crTemp = exampleTemp.createCriteria();
                    crTemp.andCategoryIdEqualTo(categoryId);
                    if (cityId!=null && cityId.intValue() != 0) {
                        crTemp.andCityIdEqualTo(cityId);
                    }
                    int total = tbUserScoreCollectMapper.countByExample(exampleTemp);
                    cacheService.initCategoryScoreRankNumToCache(cityId, categoryId, total);
                }
                cacheService.initCategoryScoreRankMsgToCache(cityId, categoryId, rs, false);
            }
        }catch (Exception e) {
            logger.error("pk分页出错,原因: {}", e);
        }finally {
            //解锁
            myRedisTemplate.delteKey(redisKey);
        }
    }

    @Override
    public void updateGameRadarChartBatch(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        TbGameRound tbGameRound = stadiumNotifyMsg.getCurrentGameRound();
        if (tbGameEvent == null || tbGameEvent.getId()<1 || tbGameRound == null || tbGameRound.getId() < 1) {
            return;
        }
        Map<Integer, Byte> maps = this.getUserScheduleType(tbGameEvent, tbGameRound);
        for (Map.Entry<Integer, Byte> entry: maps.entrySet()) {
            UpdateGameRadar updateGameRadar = new UpdateGameRadar();
            updateGameRadar.setUid(entry.getKey());
            updateGameRadar.setGameType(0);
            updateGameRadar.setType(tbGameEvent.getCategoryId());
            updateGameRadar.setIsWin(entry.getValue().intValue());
            radarChartService.updateGameRadarChart(updateGameRadar);
        }
        logger.info("每一轮完结更新雷达图,当前赛事:{},当前赛事:{},", tbGameEvent.getId(), tbGameRound.getId());
    }

    @Override
    public void generateCurrentRoundMQ(TbGameRound preTbGameRound, TbGameRound currentTbGameRound, TbGameEvent tbGameEvent,  Boolean isLast) {
        logger.info("每一轮次结束,开始调起相关的信息,当前赛事:{},轮次:{}", tbGameEvent.getId(), currentTbGameRound.getId());
        if (currentTbGameRound.getMqScoreStatus().intValue() == 0) {  //mqstatus  0 未记录 1初始化 2完结
            TbGameEvent tbGameEventTemp = new TbGameEvent();
            tbGameEventTemp.setId(tbGameEvent.getId());
            tbGameEventTemp.setCurrentRoundId(currentTbGameRound.getId());
            bizGameSystemDao.updateTbGameEvent(tbGameEventTemp);    //更新当前轮次

            if (!isLast) {
                gameMQProduct.userScheduleInitMQ(tbGameEvent, currentTbGameRound);
            }
            TbGameRound round = new TbGameRound();
            round.setId(currentTbGameRound.getId());
            round.setMqScoreStatus((byte) 1);
            bizGameSystemDao.updateTbGameRound(round);
            if (preTbGameRound!=null && preTbGameRound.getId()!=null && preTbGameRound.getMqScoreStatus().intValue() == 1) {  //mq积分计算  和mq记录用户的信息
                gameMQProduct.gameScheduleEndScoreGradeMQ(tbGameEvent, preTbGameRound);
                gameMQProduct.userScheduleFinishMQ(tbGameEvent, preTbGameRound);
                gameMQProduct.gameScheduleEndRadarChartMQ(tbGameEvent, preTbGameRound);
                gameMQProduct.gameScheduleEndRemindPushMsgMQ(tbGameEvent, preTbGameRound);

                TbGameRound round1 = new TbGameRound();
                round1.setId(preTbGameRound.getId());
                round1.setMqScoreStatus((byte) 2);
                bizGameSystemDao.updateTbGameRound(round1);
            }
        }
        if (isLast) {
            if (currentTbGameRound!=null && currentTbGameRound.getId()!=null && currentTbGameRound.getMqScoreStatus().intValue() == 1) {  //mq积分计算  和mq记录用户的信息
                gameMQProduct.gameScheduleEndScoreGradeMQ(tbGameEvent, preTbGameRound);
                gameMQProduct.userScheduleFinishMQ(tbGameEvent, preTbGameRound);
                gameMQProduct.gameScheduleEndRadarChartMQ(tbGameEvent, preTbGameRound);
                gameMQProduct.gameScheduleEndRemindPushMsgMQ(tbGameEvent, preTbGameRound);

                TbGameRound round1 = new TbGameRound();
                round1.setId(preTbGameRound.getId());
                round1.setMqScoreStatus((byte) 2);
                bizGameSystemDao.updateTbGameRound(round1);
            }
            gameMQProduct.gameEventFinish(tbGameEvent, currentTbGameRound);
        }
        logger.info("每一轮对战表生成,发送相应的mq消息,当前赛事:{},当前赛事:{},", tbGameEvent.getId(), currentTbGameRound.getId());
    }

    @Override
    public void generateCurrentRoundPushMsgAndMQ(StadiumNotifyMsg stadiumNotifyMsg) {
        TbGameEvent tbGameEvent = stadiumNotifyMsg.getTbGameEvent();
        TbGameRound tbGameRound = stadiumNotifyMsg.getCurrentGameRound();
        if (tbGameEvent == null || tbGameEvent.getId()<1 || tbGameRound == null || tbGameRound.getId() < 1) {
            return;
        }
        Integer preGameRoundId = 0;
        Integer currentGameRoundId = tbGameRound.getId();
        Byte gameSystemType = tbGameEvent.getGameSystemType();
        if (tbGameEvent.getCurrentRoundId().equals(tbGameRound.getId()))  { // 最后一轮
            preGameRoundId = tbGameRound.getId();
        }else { //非最后一轮
            List<TbGameRound> tbGameRoundList = bizGameSystemDao.selectTbGameRoundByGameEventId(tbGameEvent.getId());
            int i=0;
            for (TbGameRound roundTemp : tbGameRoundList) {    //校验轮次合法性,更新轮次的时间
                if (tbGameRound.getId().equals(roundTemp.getId())) {
                    if (i>0) {
                        preGameRoundId = tbGameRoundList.get(i-1).getId();
                    }
                    break;
                }
                i++;
            }
        }
        List<Integer> totalUids = new ArrayList<>();
        List<Integer> currentUids = new ArrayList<>();
        List<Integer> failUids = new ArrayList<>();
        List<TbGameEventTeam> totalTeams = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getId(), GameConstants.IS_NOT_DELETE);
        if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {   //俱乐部
            List<Integer> clubIds = new ArrayList<>();
            for (TbGameEventTeam tb:totalTeams) {
                clubIds.add(tb.getClubId());
            }
            Map<Integer ,List<Integer>> clugUIds = baseService.queryClubIds(clubIds);
            for (TbGameEventTeam tb:totalTeams) {
                if (null!=clugUIds.get(tb.getClubId()) && clugUIds.get(tb.getClubId()).size()>0) {
                    totalUids.addAll(clugUIds.get(tb.getClubId()));
                }
            }
        }else { //个人
            for (TbGameEventTeam tb:totalTeams) {
                totalUids.add(tb.getUid());
            }
            currentUids = totalUids;
            failUids = null;
        }



        Set<Integer> winUsers = new HashSet<>();
        Set<Integer> winClubs = new HashSet<>();
        if (preGameRoundId != 0) {
            this.generateCurrentRoundPushMsgAndMQSon1(preGameRoundId, currentUids, gameSystemType, winUsers, failUids, winClubs, tbGameEvent, currentGameRoundId);
        }
        this.generateCurrentRoundPushMsgAndMQSon2(preGameRoundId,totalUids, currentUids, winUsers, failUids, winClubs, tbGameEvent, tbGameRound);
        logger.info("每一轮对战表生成,想客户端推送相关消息,当前赛事:{},当前赛事:{},", tbGameEvent.getId(), tbGameRound.getId());
    }

    private void generateCurrentRoundPushMsgAndMQSon1(Integer preGameRoundId, List<Integer> currentUids, Byte gameSystemType,
                                                      Set<Integer> winUsers, List<Integer> failUids, Set<Integer> winClubs, TbGameEvent tbGameEvent, Integer currentGameRoundId){
        logger.info("生成晋级者,");
        if (gameSystemType.intValue() == GameSystemConfigEnum.CONFIG_GAME_TYPE_WASHED.getId() || gameSystemType.intValue() == GameSystemConfigEnum.CONFIG_GAME_TYPE_MIXED.getId()) {
            List<Integer> preUIds = new ArrayList<>();
            List<VDefinedGameSchedule> preDefinedGameSchedules = bizGameSystemDao.selectVDefinedGameScheduleByGameRoundId(preGameRoundId, null, null);
            List<VDefinedGameSchedule> curDefinedGameSchedules = bizGameSystemDao.selectVDefinedGameScheduleByGameRoundId(currentGameRoundId, null, null);

            if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {   //俱乐部
                List<Integer> clubIdsA = new ArrayList<>();
                List<Integer> clubIdsB = new ArrayList<>();
                for (VDefinedGameSchedule  v: preDefinedGameSchedules) {
                    if (v.getIsWin().intValue() == GameConstants.SCHEDULE_WIN.intValue() && v.getType().intValue() == 0) {
                        winClubs.add(v.getClubId());
                    }else if (v.getIsWin().intValue() == GameConstants.SCHEDULE_LOSE.intValue() && v.getType().intValue() == 0) {
                        winClubs.add(v.getClubId());
                    }

                    clubIdsA.add(v.getClubId());
                }
                for (VDefinedGameSchedule  v: curDefinedGameSchedules) {
                    clubIdsB.add(v.getClubId());
                }
                List<Integer> clubIdsC = clubIdsA;
                Map<Integer ,List<Integer>> clugUIds = baseService.queryClubIds(clubIdsC);
                clubIdsA.removeAll(clubIdsB);

                for (Integer tb:clubIdsC) {
                    if (null!=clugUIds.get(tb) && clugUIds.get(tb).size()>0) {
                        currentUids.addAll(clugUIds.get(tb));
                    }
                }
                for (Integer tb:clubIdsA) {
                    if (null!=clugUIds.get(tb) && clugUIds.get(tb).size()>0) {
                        failUids.addAll(clugUIds.get(tb));
                    }
                }
            }else { //个人
                for (VDefinedGameSchedule  v: preDefinedGameSchedules) {
                    if (v.getIsWin().intValue() == GameConstants.SCHEDULE_WIN.intValue() && v.getType().intValue() == 0) {
                        winUsers.add(v.getUid());
                    }else if (v.getIsWin().intValue() == GameConstants.SCHEDULE_LOSE.intValue() && v.getType().intValue() == 0) {
                        winUsers.add(v.getUid());
                    }
                    preUIds.add(v.getUid());
                }
                for (VDefinedGameSchedule  v: curDefinedGameSchedules) {
                    currentUids.add(v.getUid());
                }
                failUids = preUIds;
                failUids.removeAll(currentUids);
            }
        }
    }
    private void generateCurrentRoundPushMsgAndMQSon2(Integer preGameRoundId, List<Integer> totalUids, List<Integer> currentUids,
                                                      Set<Integer> winUsers, List<Integer> failUids, Set<Integer> winClubs, TbGameEvent tbGameEvent, TbGameRound tbGameRound){
        logger.info("生成当前轮次的消息和推送");
        if (preGameRoundId.intValue() != 0) {
            //轮次结束给用户推送消息
            if (null!=totalUids && totalUids.size() > 0) {
                gameMsgPush.pushGameRoundEndToUser(totalUids, tbGameEvent);
            }
            //推送上一轮用户晋级
            if (null!=currentUids && currentUids.size() > 0) {
                gameMsgPush.pushGameRoundEndToWinUser(currentUids, tbGameEvent);
            }


            //发送上一轮胜利个人,发送MQ到用户中心
            gameMQProduct.gameScheduleWinUserMQ(new ArrayList<Integer>(winUsers), tbGameEvent, tbGameRound);

            // 发送 胜利的 用户/俱乐部;
            gameMQProduct.gameScheduleWinUserMedalMQ(tbGameEvent.getCategoryId(), new ArrayList<Integer>(winUsers));
            gameMQProduct.gameScheduleWinClubMedalMQ(tbGameEvent.getCategoryId(), new ArrayList<Integer>(winClubs));


            //推送上一轮用户未晋级
            if (null!=failUids && failUids.size() > 0) {
                gameMsgPush.pushGameRoundEndToFailUser(failUids, tbGameEvent);
            }
        }
        if (null!=currentUids && currentUids.size() > 0) {
            //推送对战表生成
            gameMsgPush.pushGeneraterCompListToUser(currentUids, tbGameEvent);
        }
    }
    /**
     * 获取一个轮次用户的胜负
     *
     * */
    public Map<Integer, Byte> getUserScheduleType(TbGameEvent tbGameEvent, TbGameRound tbGameRound){
        Map<Integer, Byte> userScheduleStatus = new HashMap<>();
        Map<Integer, List<Integer>> mapClub = new HashMap<>();    //俱乐部下面的信息
        if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {
            List<TbGameEventTeam> tbGameEventTeamList = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getId(), GameConstants.IS_NOT_DELETE);
            List<Integer> ids = new ArrayList<>();
            for (TbGameEventTeam team : tbGameEventTeamList) {
                ids.add(team.getClubId());
            }
            mapClub = baseService.queryClubIds(ids);
        }

        List<VDefinedGameSchedule> vDefinedGameSchedules = bizGameSystemDao.selectVDefinedGameScheduleByGameRoundId(tbGameRound.getId(), null, null);
        if (null == vDefinedGameSchedules || vDefinedGameSchedules.size() < 1) {
            return userScheduleStatus;
        }
        Map<Integer, Set<VDefinedGameSchedule>> mapSchedules = new HashMap<>();
        Set<VDefinedGameSchedule> setSchedule = new HashSet<>();
        for (VDefinedGameSchedule vDefinedGameSchedule : vDefinedGameSchedules) {
            setSchedule = mapSchedules.get(vDefinedGameSchedule.getGameScheduleId());
            if (null == setSchedule || setSchedule.size() < 1) {
                setSchedule = new HashSet<>();
            }
            setSchedule.add(vDefinedGameSchedule);
            mapSchedules.put(vDefinedGameSchedule.getGameScheduleId(), setSchedule);
        }

        if (null == vDefinedGameSchedules || vDefinedGameSchedules.size() < 1) {
            return userScheduleStatus;
        }
        for (Map.Entry<Integer, Set<VDefinedGameSchedule>> entry : mapSchedules.entrySet()) {
            if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_CLUB)) {
                for (VDefinedGameSchedule vDefinedGameSchedule : entry.getValue()) {
                    if (null != mapClub && mapClub.size() > 0) {        //更新俱乐部下的每一个用户
                        List<Integer> uids = mapClub.get(vDefinedGameSchedule.getClubId());
                        if (null != uids && uids.size() > 0) {
                            for (Integer integer1 : uids) {
                                userScheduleStatus.put(integer1, vDefinedGameSchedule.getIsWin());
                            }
                        }
                    }
                }
            } else if (tbGameEvent.getGameTeamType().equals(GameConstants.TEAM_TYPE_PERSON.byteValue())) {
                for (VDefinedGameSchedule vDefinedGameSchedule : entry.getValue()) {
                    userScheduleStatus.put(vDefinedGameSchedule.getUid(), vDefinedGameSchedule.getIsWin());
                }
            } else {
                ;
            }
        }
        return userScheduleStatus;
    }

    @Override
    public List<Integer> queryUidsFromTbGameEvent(TbGameEvent tbGameEvent) {
        List<TbGameEventTeam> teams = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getId(), GameConstants.IS_NOT_DELETE);
        List<Integer> uids = new ArrayList<>();
        List<Integer> clubIds = new ArrayList<>();
        for(TbGameEventTeam tbGameEventTeam :teams) {
            if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                clubIds.add(tbGameEventTeam.getClubId());
            }else{
                uids.add(tbGameEventTeam.getUid());
            }
        }
        if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            Map<Integer, List<Integer>> map = baseService.queryClubIds(clubIds);
            Set<Integer> tempIds = new HashSet<>();
            for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
                if (null!=entry.getValue() && entry.getValue().size()>0) {
                    tempIds.addAll(entry.getValue());
                }
            }
            uids = new ArrayList<>(tempIds);
        }
        return uids;
    }

    @Override
    public void initUserCategoryScore(Integer uid, Integer cityId) {
        logger.info("开始初始化用户:{}的栏目积分", uid);
        if (null == cityId || cityId == 0) {
            cityId = GameConstants.DEFAULT_CITY_ID;
        }
        TbPoints basketPoints = new TbPoints();
        basketPoints.setCategoryId(GameConstants.CATEGORY_ID_BASKETBALL);
        basketPoints.setCityId(cityId);
        basketPoints.setUid(uid);
        basketPoints.setScore(0);
        pointsService.entryUserPoints(basketPoints);

        TbPoints badmintonPoints = new TbPoints();
        badmintonPoints.setCategoryId(GameConstants.CATEGORY_ID_BADMINTON);
        badmintonPoints.setCityId(cityId);
        badmintonPoints.setUid(uid);
        badmintonPoints.setScore(0);
        pointsService.entryUserPoints(badmintonPoints);

        TbPoints footPoints = new TbPoints();
        footPoints.setCategoryId(GameConstants.CATEGORY_ID_FOOTBALL);
        footPoints.setCityId(cityId);
        footPoints.setUid(uid);
        footPoints.setScore(0);
        pointsService.entryUserPoints(footPoints);

        TbPoints tennisPoints = new TbPoints();
        tennisPoints.setCategoryId(GameConstants.CATEGORY_ID_TENNIS);
        tennisPoints.setCityId(cityId);
        tennisPoints.setUid(uid);
        tennisPoints.setScore(0);
        pointsService.entryUserPoints(tennisPoints);
    }


    /**
     * 批量获取某栏目下用户的栏目积分
     *
     * */
    private Map<Integer, TbPoints> batchQueryUserCategoryScore(List<Integer> uids, Integer categoryId){
        Map<Integer, TbPoints> userPointsMap = new HashMap<>();
        if (uids.size()>0 && categoryId >0) {
            List<TbPoints> userPointsList = pointsService.queryPointsByCategoryIdAndUserIds(uids, categoryId);
            if (null!=userPointsList && userPointsList.size() >0) {
                for (TbPoints tbPoints:userPointsList) {
                    userPointsMap.put(tbPoints.getUid(), tbPoints);
                }
            }
        }
        return userPointsMap;
    }

    /**
     * 赛事下积分公式计算   (废弃!!!!!!)
     *
     * */

    @SuppressWarnings("unused")
    @Deprecated
    private Integer integralUserPkScore(List<Integer> fromScore, List<Integer> toScore, Integer result) {

        int score1=0,score2=0,i;
        for (i=0; i<fromScore.size(); i++) {
            score1 += fromScore.get(i);
            if (toScore.size() > i) {
                score2 += toScore.get(i);
            }
        }
        Double we = 0.0;
        double w = 0.0;
        return gradeIntegralService.integralCalulate(w, we.intValue());
    }

    /**
     * 赛事下积分增加  (废弃 !!!!!)
     *
     * */

    @SuppressWarnings("unused")
    @Deprecated
    private void modifyUserScore(Integer uid, int score, int categoryId) {
        // pk胜利者积分录入
        if (uid == null || uid < 1) {
            return;
        }
        TbPoints tbPoints = new TbPoints();
        tbPoints.setCategoryId(categoryId);
        tbPoints.setUid(uid);
        tbPoints.setScore(score);
        boolean flag = pointsService.entryUserPoints(tbPoints);
        logger.info("entryPKResult winner user id:"+tbPoints.getUid()+" score:"+tbPoints.getScore()+" flag:"+ flag);
    }

    /**
     * 推送官方赛事可参与人
     *
     * */

    private void pushJoinAccessUser(List<Integer> uids, List<Integer> clubIds, TbGameEvent tbGameEvent) {
        if (null == uids || uids.size()<1 || null == clubIds || clubIds.size() < 1) {
            return;
        }
        if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            Map<Integer, List<Integer>> users = baseService.queryClubIds(clubIds);
            Set<Integer> tempList = new HashSet<>();
           for (Map.Entry<Integer, List<Integer>> entry : users.entrySet()) {
               if (null!=entry.getValue() && entry.getValue().size()>0) {
                   tempList.addAll(entry.getValue());
               }
           }
            uids = new ArrayList<>(tempList);
        }
        // 推送官方赛事可参与人
        gameMsgPush.pushJoinOfficialGameEventAccessUser(tbGameEvent, uids);
    }
}
